#include "StdAfx.h"
#include ".\cstring.h"

cstring::cstring(void):readoff(0)
{
}

cstring::~cstring(void)
{
}

unsigned int cstring::size() { 
	return str.size();
	}
	
unsigned int cstring::appendUCHAR( unsigned char p) {
	str.append(1, p);
	return size();
}

unsigned int cstring::appendChars( char *p , unsigned int len) {
	str.append(p, len);
	return size();
}

unsigned int cstring::appendString( string str1 , unsigned int len ) {
	if (len != 0) {
		if (len < str1.size()) {
			str.append(str1, 0, len);
			return size();
		} else {
			str.append(str1);
			unsigned int b = len-str1.size();
			str.append(b, '\0');
			return size();
		}
	} else {
		str.append(str1);
		return size();
	}
}

unsigned int cstring::appendUINT( UINT iNum) {
	union {
		UINT a;
		char p[4];
	} s;
	s.a = htonl(iNum);
	str.append(s.p, 4);
	return size();
}

unsigned int cstring::getUINT(unsigned int &pRc) {
	pRc = ntohl(*(unsigned int*)(str.c_str() + readoff ));

	readoff += 4;
	return readoff;
}

unsigned int cstring::getUCHAR( unsigned char &pRc ) {
	pRc = *(str.c_str() + readoff );
	readoff++;
	return readoff;
}

unsigned int cstring::getChars( char *pRc  , unsigned int len) {
	memcpy( pRc , str.c_str() + readoff , len );
	readoff += len;
	return readoff;
}

unsigned int cstring::getString( string &str1 , unsigned int len ) {
	if( len == 0) {
		str1 = str.substr( readoff ) ;
	} else {
		str1 = str.substr( readoff , len );
	}
	readoff += str1.size() ;
	//cout << readoff << endl;
	return readoff;
}

unsigned int cstring::zero() {
	str = "";
	readoff = 0;
	return 0;
}

unsigned int cstring::dump() {
	unsigned int i;
	for ( i = 0 ; i < size() ; i++) {
		printf( "%0x " , *(str.c_str()+i) );
	}

	return 0;
}
